package guda.shop.core.manager.impl;

import guda.shop.common.hibernate3.Updater;
import guda.shop.common.page.Pagination;
import guda.shop.common.security.UsernameNotFoundException;
import guda.shop.common.security.userdetails.UserDetails;
import guda.shop.common.security.userdetails.UserDetailsService;
import guda.shop.core.dao.MemberDao;
import guda.shop.core.entity.Member;
import guda.shop.core.entity.User;
import guda.shop.core.entity.Website;
import guda.shop.core.manager.MemberMng;
import guda.shop.core.manager.UserMng;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Timestamp;

@Service
@Transactional
public class MemberMngImpl
        implements MemberMng, UserDetailsService {
    private UserMng userMng;
    private MemberDao memberDao;

    public UserDetails loadUser(Long paramLong, String paramString)
            throws UsernameNotFoundException {
        Member localMember = findById(paramLong);
        if (localMember == null)
            throw new UsernameNotFoundException("member id not found '" + paramLong + "'");
        return localMember;
    }

    public Member getByUsername(Long paramLong, String paramString) {
        User localUser = this.userMng.getByUsername(paramString);
        if (localUser == null)
            return null;
        return getByUserId(paramLong, localUser.getId());
    }

    public Member getByUserIdAndActive(String paramString, Long paramLong) {
        return this.memberDao.getByUserIdAndActive(paramString, paramLong);
    }

    public Member getByUserId(Long paramLong1, Long paramLong2) {
        return this.memberDao.getByUserId(paramLong1, paramLong2);
    }

    public Member register(String paramString1, String paramString2, String paramString3, Boolean paramBoolean1, String paramString4, String paramString5, Boolean paramBoolean2, Website paramWebsite) {
        Member localMember = new Member();
        Timestamp localTimestamp = new Timestamp(System.currentTimeMillis());
        User localUser = this.userMng.register(paramString1, paramString2, paramString3, paramString5, localTimestamp);
        localMember.setCreateTime(localTimestamp);
        localMember.setUser(localUser);
        localMember.setWebsite(paramWebsite);
        localMember.setDisabled(paramBoolean2);
        localMember.setActive(paramBoolean1);
        localMember.setActivationCode(paramString4);
        return save(localMember);
    }

    public Member join(String paramString, Website paramWebsite) {
        User localUser = this.userMng.getByUsername(paramString);
        if (localUser == null)
            throw new IllegalStateException("User not found: " + paramString);
        return join(localUser, paramWebsite);
    }

    public Member join(User paramUser, Website paramWebsite) {
        Member localMember1 = getByUserId(paramWebsite.getId(), paramUser.getId());
        if (localMember1 != null)
            return localMember1;
        Member localMember2 = new Member();
        localMember2.setUser(paramUser);
        localMember2.setWebsite(paramWebsite);
        return save(localMember2);
    }

    public Pagination getPage(int paramInt1, int paramInt2) {
        return this.memberDao.getPage(paramInt1, paramInt2);
    }

    public Member findById(Long paramLong) {
        return this.memberDao.findById(paramLong);
    }

    public Member save(Member paramMember) {
        paramMember.init();
        return this.memberDao.save(paramMember);
    }

    public Member update(Member paramMember) {
        return this.memberDao.updateByUpdater(new Updater(paramMember));
    }

    public Member deleteById(Long paramLong) {
        return this.memberDao.deleteById(paramLong);
    }

    public Member[] deleteByIds(Long[] paramArrayOfLong) {
        Member[] arrayOfMember = new Member[paramArrayOfLong.length];
        for (int i = 0; i < paramArrayOfLong.length; i++)
            arrayOfMember[i] = deleteById(paramArrayOfLong[i]);
        return arrayOfMember;
    }

    @Autowired
    public void setDao(MemberDao paramMemberDao) {
        this.memberDao = paramMemberDao;
    }

    @Autowired
    public void setUserMng(UserMng paramUserMng) {
        this.userMng = paramUserMng;
    }
}

